//
// Created by chen on 2023/6/23.
//
#include "manager.h"

Manager::Manager(string name) {
    this->m_name = name;
}

Manager::~Manager() {

}

void Manager::showMenu() {
    cout << "****************************************" << endl;
    cout << "***********欢迎使用机房预约系统************" << endl;
    cout << "***********   1、添加账号    ************" << endl;
    cout << "***********   2、查看账号    ************" << endl;
    cout << "***********   3、查看机房    ************" << endl;
    cout << "***********   4、清空预约    ************" << endl;
    cout << "***********   0、注销登录    ************" << endl;
    cout << "***************************************" << endl;\
    cout << "请选择:";
}

void saveToCsv(vector<string> v, string fileName){
    ofstream ofs;
    ofs.open(fileName, ios::app);
    if(!ofs.is_open()){
        cout << "文件打开失败, 账号添加失败" << endl;
        return;
    }
    for (vector<string>::iterator it= v.begin(); it != v.end() ; ++it) {
        ofs << *it << ",";
    }
    ofs << endl;
    ofs.close();
    cout << "账号添加成功！" << endl;
}

void addStudentAccount(){
    string id;
    string name;
    string passwd;
    cout << "请输入学生id:";
    cin >> id;
    cout << "请输入学生姓名:";
    cin >> name;
    cout << "请输入密码:";
    cin >> passwd;
    vector<string> temp;
    temp.push_back(id);
    temp.push_back(name);
    temp.push_back(passwd);
    saveToCsv(temp, STUDENTFILE);
}

void addTeacherAccount(){
    string id;
    string name;
    string passwd;
    cout << "请输入教师工号:";
    cin >> id;
    cout << "请输入教师姓名:";
    cin >> name;
    cout << "请输入密码:";
    cin >> passwd;
    vector<string> temp;
    temp.push_back(id);
    temp.push_back(name);
    temp.push_back(passwd);
    saveToCsv(temp, TEACHERFILE);
}

void addManagerAccount(){
    string name;
    string passwd;
    cout << "请输入管理员姓名:";
    cin >> name;
    cout << "请输入密码:";
    cin >> passwd;
    vector<string> temp;
    temp.push_back(name);
    temp.push_back(passwd);
    saveToCsv(temp, MANAGERFILE);
}

void Manager::addAccount() {
    string AccountType;
    cout << "请选择要添加的账号类型(1:学生,2:教师,0:管理员):";
    cin >> AccountType;
    if (AccountType == "1"){
        addStudentAccount();
    } else if(AccountType == "2"){
        addTeacherAccount();
    } else if (AccountType == "0"){
        addManagerAccount();
    }else{
        cout << "无效类型！" << endl;
    }
}

void Manager::viewStudentAccountInfo() {
    string id;
    cout << "请输入学生id:";
    cin >> id;
    map<string , map<string, string>>::iterator it= at.studentPasswd.find(id);
    if (it == at.studentPasswd.end()){
        cout << "无此学生账号" << endl;
    } else{
        map<string, string>::iterator it2= it->second.begin();
        cout << "id=" << id << ", name=" <<it2->first << ",passwd=" << it2->second << endl;
    }
}

void Manager::viewTeacherAccountInfo() {
    string id;
    cout << "请输入教师工号:";
    cin >> id;
    map<string , map<string, string>>::iterator it= at.teacherPasswd.find(id);
    if (it == at.teacherPasswd.end()){
        cout << "无此教师账号" << endl;
    } else{
        map<string, string>::iterator it2= it->second.begin();
        cout << "id=" << id << ", name=" <<it2->first << ",passwd=" << it2->second << endl;
    }
}

void Manager::viewManagerAccountInfo() {
    string name;
    cout << "请输入管理员姓名:";
    cin >> name;
    map<string , string>::iterator it= at.managerPasswd.find(name);
    if (it == at.managerPasswd.end()){
        cout << "无此管理员账号" << endl;
    } else{
        cout << "name=" <<it->first << ",passwd=" << it->second << endl;
    }
}

void Manager::viewAccountInfo() {
    string AccountType;
    cout << "请选择要查看的账号类型(1:学生,2:教师,0:管理员):";
    cin >> AccountType;
    if (AccountType == "1"){
        this->viewStudentAccountInfo();
    } else if(AccountType == "2"){
        this->viewTeacherAccountInfo();
    } else if (AccountType == "0"){
        this->viewManagerAccountInfo();
    }else{
        cout << "无效类型！" << endl;
    }
}

void Manager::viewRoomInfo() {
    MachineRoom mr;
    mr.viewRoomInfo();
}

void Manager::clearAllReservation() {
    MachineRoom mr;
    mr.clearAllReservation();
}

void Manager::logOut() {
    cout << "已注销登录" << endl;
}

